package com.cmz.common.dto;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.common.collect.Lists;
import com.cmz.common.api.dto.StandardPagination;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * 通用分页响应
 * @param <T> 响应数据条目类型
 * @author cmz
 */
@Getter
@Setter
@ApiModel("通用分页响应")
public class GridResponse<T> extends Response<GridResponse.GridData<T>> {

    /** 分页响应数据 */
    @ApiModelProperty(name = "data", value = "分页响应数据")
    private GridData<T> data = new GridData<>();

    /**
     * 分页响应数据
     * @param <T> 响应数据类型
     */
    @Getter
    @Setter
    @ApiModel("分页请求数据")
    public static class GridData<T> {

        /** 数据集合 */
        @ApiModelProperty(name = "datalist", value = "数据集合")
        private List<T> datalist = Lists.newArrayList();

        /** 分页信息 */
        @ApiModelProperty(name = "page", value = "分页信息")
        @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
        private StandardPagination page;

    }

    public static <T> GridResponse<T> newInstance(List<T> datalist, StandardPagination page) {
        GridData<T> data = new GridData<>();
        data.setDatalist(datalist);
        data.setPage(page);

        GridResponse<T> response = new GridResponse<>();
        response.setData(data);

        return response;
    }

    public static <T> GridResponse<T> newInstance(List<T> datalist) {
        GridData<T> data = new GridData<>();
        data.setDatalist(datalist);

        GridResponse<T> response = new GridResponse<>();
        response.setData(data);

        return response;
    }

}
